package com.assignwork.service.impl;

import com.assignwork.dto.WorkLineLogInfoDTO;
import com.assignwork.dto.WorkLineLogMachineDTO;
import com.assignwork.vo.CargoCategoryVO;
import com.assignwork.vo.WorkLineLogInfoVO;
import com.common.cache.AreaSettingsCache;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.system.cache.DictBizCache;
import org.springframework.stereotype.Service;
import org.springblade.core.mp.base.BaseServiceImpl;
import com.assignwork.entity.WorkLineLogInfo;
import com.assignwork.mapper.WorkLineLogInfoMapper;
import com.assignwork.service.IWorkLineLogInfoService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (WorkLineLogInfo)表服务实现类
 */
@AllArgsConstructor 
@Service("workLineLogInfoService")
public class WorkLineLogInfoServiceImpl extends BaseServiceImpl<WorkLineLogInfoMapper, WorkLineLogInfo> implements IWorkLineLogInfoService {

    @Override
    public boolean saveOrUpdateDto(List<WorkLineLogInfoDTO> info) {
        return this.saveOrUpdateBatch(WorkLineLogInfoDTO.toEtt(info));
    }

    @Override
    public List<WorkLineLogInfoVO> listVoByArrangeId(Long arrangeId) {
        List<WorkLineLogInfoVO> ls = baseMapper.listVoByArrangeId(arrangeId);
        for (WorkLineLogInfoVO info : ls) {
            List<String> cabinKey = Func.toStrList(info.getCabinKey());
            List<String> cabinName = new ArrayList<>();
            Kv cabin = Kv.create();
            for (String s : cabinKey) {
                String name = DictBizCache.getValue("hold", s);
                cabin.put(s, name);
                cabinName.add(name);
            }
            info.setCabin(cabin);
            info.setCabinName(Func.join(cabinName));

//            List<CargoCategoryVO> cargoList = info.getCargoList();
//            if (cargoList != null) {
//                List<String> cargoTypeName = new ArrayList<>();
//                Kv cargo = Kv.create();
//                for (CargoCategoryVO c : cargoList) {
//                    cargo.put(c.getCode(), c.getName());
//                    cargoTypeName.add(c.getName());
//                }
//                info.setCargoType(cargo);
//                info.setCargoTypeName(Func.join(cargoTypeName));
//            }
            String workPlaceKey = info.getWorkPlaceKey();
            if (workPlaceKey != null) {
                List<String> name = new ArrayList<>();
                List<String> strList = Func.toStrList(workPlaceKey);
                Kv wp = Kv.create();
                for (String s : strList) {
                    Long id = Func.toLong(s);
                    if (id != null && id > 0) {
                        String value = AreaSettingsCache.getValue(id);
                        name.add(value);
                        wp.put(s, value);
                    } else {
                        name.add(DictBizCache.getValue("berth", info.getWorkPlaceKey()));
                    }
                }
                info.setWorkPlace(wp);
                info.setWorkPlaceName(Func.join(name));
            }

            info.setBerthName(DictBizCache.getValue("berth", info.getBerthKey()));
            info.setProcessKeyName(DictBizCache.getValue("operation_process", info.getProcessKey()));
            info.setLoadTypeKeyName(DictBizCache.getValue("load_type", info.getLoadTypeKey()));
            info.setPackageBulkKeyName(DictBizCache.getValue("package_bulk", info.getPackageBulkKey()));
            info.setLandWorkTypeKeyName(DictBizCache.getValue("land_work_type", info.getLandWorkTypeKey()));
        }
        return ls;
    }
}
